package com.dk.controller;

import com.dk.pojo.News;
import com.dk.pojo.NewsType;
import com.dk.service.NewTypeService;
import com.dk.service.NewsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("news")
public class NewsController {
    @Autowired
    private NewsService newsService;
    @Autowired
    private NewTypeService newTypeService;
    @RequestMapping("getInfo")
    @ResponseBody
    public PageInfo<News> getInfo(News news, @RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize){
        PageInfo<News> page = newsService.selectInfo(news,pageNum,pageSize);
        return page;
    }
    @RequestMapping("toNews")
    public String toNews(Model model) {
       List<NewsType> list = newTypeService.selectInfo();
       model.addAttribute("list",list);
        return "news/news";
    }
   @RequestMapping("toIncrease")
   @ResponseBody
    public Integer increase(News news) {
       Integer i = newsService.insertInfo(news);
       return i;

   }
   @RequestMapping("toDelete")
    public String toDelete(Integer newsId){
        Integer i = newsService.deleteById(newsId);
        return "redirect:/news/toNews";
   }
   @RequestMapping("toUpdate")
    public String toUpdate(Integer newsId,Model model){
        News news = newsService.selectById(newsId);
        model.addAttribute("n",news);
        return "news/update";
   }
   @RequestMapping("newsUpdate")
   @ResponseBody
    public Integer updateInfo(News news){
       Integer integer = newsService.updateInfo(news);
       return integer;
   }
   @RequestMapping("deleteBath")

    public String deleteBath(Integer[] array){
        Integer i = newsService.deleteByIds(array);
       return "redirect:/news/toNews";
   }
   @RequestMapping("toClient")
    public String toClient(){

        return "client";
   }
    @RequestMapping("getInfoClient")
    @ResponseBody
    public List<News> getInfoClient(News news){
        List<News> list = newsService.getInfoClient(news);
        return list;
    }
    @RequestMapping("toServer")
    public String  toServer(Model model ){
        List<NewsType> list = newTypeService.selectInfo();
        model.addAttribute("list",list);
        return "server";
    }
    @RequestMapping("getInfoServer")
    @ResponseBody
    public Map<String,Object> getInfoServer(News news,@RequestParam(defaultValue = "1")Integer pageNumber,
                                         @RequestParam(defaultValue = "3")Integer pageSize){
      PageInfo<News> page = newsService.getInfoServer(news,pageNumber,pageSize);
      Map<String,Object> map = new HashMap<>();
      map.put("total",page.getTotal());
      map.put("rows",page.getList());
      return map;
    }
   @RequestMapping("deletes")
   @ResponseBody
    public String deleteBatchs(Integer[] array){
       Integer i = newsService.deleteBatchs(array);
        return null;
   }
   @RequestMapping("deleteByIds")
    @ResponseBody
    public Integer deleteByIdss(Integer newsId){
       return  newsService.deleteById(newsId);
   }
   @RequestMapping("toadd")
    public String toadd(){
        return "news/add";
   }
   @RequestMapping("toNewsUpdate/{id}")
    public String toNewsUpdate(@PathVariable("id")Integer id, Model model){
       News news = newsService.selectById(id);
       model.addAttribute("news",news);
       return "news/newsUpdate";
   }

}
